home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / vmed.arc / ED4.CCC < prev    next >
Encoding:
Text File  |  1985-12-03  |  11.3 KB  |  505 lines

  1. /*    Screen editor:    window module
  2.  *
  3.  *    Module: ed4/ccc
  4.  *    Date: November 12, 1983
  5.  *    Changed: Feb. 27, 1984    -- error test for edjoin()
  6.  *    Changed: March 8, 1984
  7.  *    Last Changed: March 10, 1984
  8.  */
  9.  
  10. #include ed0
  11.  
  12. /* data global to this module */
  13.  
  14. char    editbuf[MAXLEN];    /* the edit buffer */
  15. int    editp,    /* cursor: buffer index */
  16.     editpmax,    /* length of buffer */
  17.     edcflag;    /* buffer change flag */
  18.  
  19. /* abort any changes made to current line */
  20. edabt()
  21. {    /* get unchanged line and reset cursor */
  22.     edgetln();
  23.     edredraw();
  24.     edbegin();
  25.     edcflag=NO;
  26. }
  27.  
  28. /* put cursor at beginning of current line */
  29. edbegin()
  30. {    editp=0;
  31.     outxy(0,outgety());
  32. }
  33.  
  34. /* change editbuf[editp] to c
  35.  * don't make change if line would become too long
  36.  */
  37. edchng(c)    char c;
  38. {    char    oldc;
  39.     int    k;
  40. /* if at right margin then insert char */
  41.     if (editp >= editpmax)    {
  42.         edins(c);
  43.         return;
  44.     }
  45. /* change char and adjust length of line */
  46.     oldc = editbuf[editp];
  47.     editbuf[editp] = c;
  48.     fmtadj(editbuf,editp,editpmax);
  49. /* set change flag, redraw line */
  50.     edcflag=YES;
  51.     ++editp;
  52.     edredraw();
  53. }
  54.  
  55. /* delete the char to left of cursor if it exists */
  56. eddel()
  57. {    int    k;
  58. /* just move left one column if past end of line */
  59.     if (edxpos() < outgetx())    {
  60.         outxy(outgetx()-1,outgety());
  61.         return;
  62.     }
  63. /* do nothing if cursor is at left margin */
  64.     if (editp==0)        return;
  65.     edcflag=YES;
  66.     k = editp;
  67.     while (k < editpmax)
  68.         editbuf[k-1] = editbuf[k++];
  69. /* update pointers, redraw line */
  70.     --editp;
  71.     --editpmax;
  72.     edredraw();
  73. }
  74.  
  75. /* edit the next line. do not go to end of buffer */
  76. eddn()
  77. {    int    oldx;
  78. /* save visual position of cursor */
  79.     oldx = outgetx();
  80. /* replace current edit line */
  81.     if (edrepl())        return(ERR);
  82. /* do not go past last non-null line */
  83.     if (at_eof())        return(OK);        /* 3-7-84 */
  84. /* move down one line in buffer */
  85.     if (bufdn())        return(ERR);
  86.     edgetln();
  87. /* put cursor as close as possible on this new line
  88.  * to where it was on the old line.
  89.  */
  90.     editp = edscan(oldx);
  91. /* update screen */
  92.     if (edatbot())    {
  93.         edsup(bufln()-SCRNL2);
  94.         outxy(oldx,SCRNL1);
  95.     }
  96.     else    outxy(oldx,outgety()+1);
  97.     return(OK);
  98. }
  99.  
  100. /* put cursor at the end of the current line */
  101. edend()
  102. {    editp = editpmax;
  103.     outxy(edxpos(),outgety());
  104. }
  105.  
  106. /* start editing line n
  107.  * redraw the screen with cursor at position p
  108.  */
  109. edgo(n,p)    int n,p;
  110. {    n = max(1, min(n, buffree()));    /* 3-7-84 stay in file */
  111. /* replace current line */
  112.     if (edrepl()==ERR)        return(ERR);
  113. /* go to new line */
  114.     if (bufgo(n)==ERR)        return(ERR);
  115. /* redraw the screen */
  116.     bufout(bufln(),1,SCRNL1);
  117.     edgetln();
  118.     editp = min(p,editpmax);
  119.     outxy(edxpos(),1);
  120.     return(OK);
  121. }
  122.  
  123. /* insert c into the buffer if possible */
  124. edins(c)    char c;
  125. {    int    k;
  126. /* do nothing if edit buffer is full */
  127.     if (editpmax >= MAXLEN)        return;
  128. /* fill out line if we are past its end */
  129.     if ((editp == editpmax) & (edxpos() < outgetx()))    {
  130.         k = outgetx() - edxpos();
  131.         editpmax = editpmax + k;
  132.         while (k-- > 0)
  133.             editbuf[editp++] = ' ';
  134.         editp = editpmax;
  135.     }
  136. /* make room for inserted character */
  137.     k = editpmax;
  138.     while (k > editp)    {
  139.         editbuf[k] = editbuf[k-1];
  140.         --k;
  141.     }
  142. /* insert character. update pointers */
  143.     editbuf[editp] = c;
  144.     ++editp;
  145.     ++editpmax;
  146. /* recalculate print length of line */
  147.     fmtadj(editbuf,editp-1,editpmax);
  148. /* set change flag, redraw line */
  149.     edcflag = YES;
  150.     edredraw();
  151. }
  152.  
  153. /* join (concatenate) current line and one above it */
  154. edjoin()
  155. {    int    k,oldk;
  156. /* do nothing if at top of file */
  157.     if (bufattop())        return;
  158. /* replace lower line temporarily */
  159.     oldk = editpmax;
  160.     if (edrepl())        return;
  161. /* get upper line into buffer */
  162.     if (bufup())        return;
  163.     k = bufgetln(editbuf,MAXLEN);
  164.     if (bufdn())        return;
  165.     if ((k+oldk) > MAXLEN)    {        /* too big! */
  166.         edgetln();
  167.         error ("line too long");
  168.         return;
  169.     }
  170. /* append lower line to buffer */
  171.     k = k + bufgetln(editbuf+k,MAXLEN-k);
  172. /* replace upper line */
  173.     if (bufup())        return;
  174.     editpmax = k;
  175.     edcflag = YES;
  176.     if (edrepl())        return;
  177. /* delete the lower line */
  178.     if (bufdn())        return;
  179.     if (bufdel())        return;
  180.     if (bufup())        return;
  181. /* update the screen */
  182.     if (edattop())
  183.         edredraw();
  184.     else    {
  185.         k = outgety() - 1;
  186.         bufout(bufln(),k,SCRNL-k);
  187.         outxy(0,k);
  188.         edredraw();
  189.     }
  190. }
  191.  
  192. /* delete chars until end of line or c found */
  193. edkill(c)    char c;
  194. {    int    k,p;
  195. /* do nothing if at right margin */
  196.     if (editp == editpmax)        return;
  197.     edcflag = YES;
  198. /* count number of deleted characters */
  199.     k = 1;
  200.     while ((editp + k) < editpmax)
  201.         if (editbuf[editp+k] == c)
  202.             break;
  203.         else    ++k;
  204. /* compress buffer (delete characters) */
  205.     p = editp + k;
  206.     while (p < editpmax)
  207.         editbuf[p-k] = editbuf[p++];
  208. /* update buffer size, redraw line */
  209.     editpmax = editpmax - k;
  210.     edredraw();
  211. }
  212.  
  213. /* move cursor left one column.
  214.  * never move the cursor off the current line
  215.  */
  216. edleft()
  217. {    int    k;
  218. /* if past right margin, move left one column  */
  219.     if (edxpos() < outgetx())
  220.         outxy(max(0,outgetx()-1),outgety());
  221. /* inside the line. move left one character */
  222.     else if (editp)    {
  223.         --editp;
  224.         outxy(edxpos(),outgety());
  225.     }
  226. }
  227.  
  228. /* insert a new blank line below the current line */
  229. ednewdn()
  230. {    int    k;
  231. /* make sure there is a current line and
  232.  * put the current line back into the buffer.
  233.  */
  234.     if (edrepl())                return;        /* 3-7-84 */
  235. /* move past current line */
  236.     if (in_txt() && bufdn())    return;        /* 3-8-84 */
  237. /* insert place holder: zero length line */
  238.     if (bufins(editbuf,0))        return;
  239. /* start editing the zero length line */
  240.     edgetln();
  241. /* update the screen */
  242.     if (edatbot())    {        /* note: bufln() >= SCRNL */
  243.         edsup(bufln()-SCRNL2);
  244.         outxy(edxpos(),SCRNL1);
  245.     }
  246.     else    {
  247.         k = outgety();
  248.         bufout(bufln(),k+1,SCRNL1-k);
  249.         outxy(edxpos(),k+1);
  250.     }
  251. }
  252.  
  253. /* insert a new blank line above the current line */
  254. ednewup()
  255. {    int    k;
  256. /* put current line back into buffer */
  257.     if (edrepl())                return;
  258. /* insert zero length line at cursor line */
  259.     if (bufins(editbuf,0))        return;
  260. /* start editing the zero length line */
  261.     edgetln();
  262. /* update the screen */
  263.     if (edattop())    {
  264.         edsdn(bufln());
  265.         outxy(edxpos(),1);
  266.     }
  267.     else    {
  268.         k = outgety();
  269.         bufout(bufln(),k,SCRNL-k);
  270.         outxy(edxpos(),k);
  271.     }
  272. }
  273.  
  274. /* move cursor right one character.
  275.  * never move the cursor off the current line
  276.  */
  277. edright()
  278. {    /* if outside the line move right one column */
  279.     if (edxpos() < outgetx())
  280.         outxy(min(SCRNW1,outgetx()+1),outgety());
  281. /* if we are inside a tab move to the end of it */
  282.     else if (edxpos() > outgetx())
  283.         outxy(edxpos(),outgety());
  284. /* move right one character if inside line */
  285.     else if (editp < editpmax)    {
  286.         ++editp;
  287.         outxy(edxpos(),outgety());
  288.     }
  289. /* else move past end of line */
  290.     else    outxy(min(SCRNW1,outgetx()+1),outgety());
  291. }
  292.  
  293. /* split the current line into two parts.
  294.  * scroll the first half of the old line up.
  295.  */
  296. edsplit()
  297. {    int    p,q,k;
  298. /* replace current line by the first half of line 
  299.     CHANGED 3-7-84 */
  300.     if (past_eof() || bufrepl(editbuf,editp))
  301.         return;
  302. /* redraw the first half of the line */
  303.     p = editpmax;
  304.     q = editp;
  305.     editpmax = editp;
  306.     editp = 0;
  307.     edredraw();
  308. /* move the second half of the line down */
  309.     editp = 0;
  310.     while (q < p)
  311.         editbuf[editp++] = editbuf[q++];
  312.     editpmax = editp;
  313.     editp = 0;
  314. /* insert second half of the line below the first */
  315.     if (bufdn())        return;
  316.     if (bufins(editbuf,editpmax))        return;
  317. /* scroll the screen up and draw the second half */
  318.     if (edatbot())    {
  319.         edsup(bufln() - SCRNL2);
  320.         outxy(1,SCRNL1);
  321.         edredraw();
  322.     }
  323.     else    {
  324.         k = outgety();
  325.         bufout(bufln(),k+1,SCRNL1-k);
  326.         outxy(1,k+1);
  327.         edredraw();
  328.     }
  329. }
  330.  
  331. /* move cursor right until end of line
  332.  * or character c found.
  333.  */
  334. edsrch(c)    char c;
  335. {    /* do nothing if at right margin */
  336.     if (editp == editpmax)
  337.         return;
  338. /* scan for search character */
  339.     ++editp;
  340.     while (editp < editpmax)
  341.         if (editbuf[editp] == c)
  342.             break;
  343.         else    ++editp;
  344. /* reset cursor */
  345.     outxy(edxpos(),outgety());
  346. }
  347.  
  348. /* move cursor up one line if possible */
  349. edup()
  350. {    int    oldx;
  351. /* save visual position of cursor */
  352.     oldx = outgetx();
  353. /* put current line back in buffer */
  354.     if (edrepl())        return(ERR);
  355. /* done if at top of buffer */
  356.     if (bufattop())        return(OK);
  357. /* start editing the previous line */
  358.     if (bufup())        return(ERR);
  359.     edgetln();
  360. /* put cursor on this new line as close as possible
  361.  * to where it was on the old line.
  362.  */
  363.     editp = edscan(oldx);
  364. /* update screen */
  365.     if (edattop())    {
  366.         edsdn(bufln());
  367.         outxy(oldx,1);
  368.     }
  369.     else    outxy(oldx,outgety()-1);
  370.     return(OK);
  371. }
  372.  
  373. /* delete the current line */
  374. edzap()
  375. {    int    k;
  376. /* delete the line in the buffer */
  377.     if (bufdel())        return;
  378. /* move up one line if now at bottom */
  379.     if (at_eof())    {            /* 3-7-84 */
  380.         if (bufup())    return;
  381.         edgetln();
  382. /* update screen */
  383.         if (edattop())
  384.             edredraw();
  385.         else    {
  386.             outdelln();
  387.             outxy(0,outgety()-1);
  388.         }
  389.         return;
  390.     }
  391. /* start editing new line */
  392.     edgetln();
  393. /* update screen */
  394.     if (edattop())    {
  395.         edsup(bufln());
  396.         outxy(0,1);
  397.     }
  398.     else    {
  399.         k = outgety();
  400.         bufout(bufln(),k,SCRNL-k);
  401.         outxy(0,k);
  402.     }
  403. }
  404.  
  405. /* return true if the current edit line is being
  406.  * displayed on the bottom line of the screen.
  407.  */
  408. edatbot()
  409. {    return(outgety() == SCRNL1);    }
  410.  
  411. /* return true if the current edit line is being
  412.  * displayed on the top line of the screen.
  413.  */
  414. edattop()
  415. {    return(outgety() == 1);    }
  416.  
  417. /* redraw edit line from index to end of line
  418.  * reposition cursor
  419.  */
  420. edredraw()
  421. {    fmtadj(editbuf,0,editpmax);
  422.     fmtsubs(editbuf,max(0,editp-1),editpmax);
  423.     outxy(edxpos(),outgety());
  424. }
  425.  
  426. /* return the x position of the cursor on screen */
  427. edxpos()
  428. {    return(min(SCRNW1,fmtlen(editbuf,editp)));    }
  429.  
  430. /* fill edit buffer from current main buffer line.
  431.  * the caller must check to make sure the main
  432.  * buffer is available.
  433.  */
  434. edgetln()
  435. {    int    k;
  436. /* put cursor on left margin, reset flag */
  437.     editp = 0;
  438.     edcflag = NO;
  439. /* get edit line from main buffer */
  440.     k = bufgetln(editbuf,MAXLEN);
  441.     if (k > MAXLEN)    {
  442.         error("line truncated");
  443.         editpmax = MAXLEN;
  444.     }
  445.     else    editpmax = k;
  446.     fmtadj(editbuf,0,editpmax);
  447. }
  448.  
  449. /* replace current main buffer line by edit buffer.
  450.  * the edit buffer is NOT changed or cleared.
  451.  * return ERR if something goes wrong.
  452.  */
  453. edrepl()
  454. {    /* do nothing if nothing has changed */
  455.     if (edcflag == NO)        return(OK);
  456. /* make sure we don't replace the line twice */
  457.     edcflag = NO;
  458. /* insert instead of replace if at bottom of file */
  459. /*    if (past_eof())        /* 3-8-84 */
  460. /*        bufins(editbuf,0);            out 3-10-84 */
  461.     return(bufrepl(editbuf,editpmax));
  462. }
  463.  
  464. /* set editp to the largest index such that buf[editp]
  465.  * will be printed <= xpos
  466.  */
  467. edscan(xpos)    int xpos;
  468. {    editp = 0;
  469.     while (editp < editpmax)    {
  470.         if (fmtlen(editbuf,editp) < xpos)
  471.             ++editp;
  472.         else    break;
  473.     }
  474.     return(editp);
  475. }
  476.  
  477. /* scroll the screen up. topline will be new top line */
  478. edsup(topline)    int topline;
  479. {    if (outhasup() == YES)    {
  480. /* hardware scroll */
  481.         outsup();
  482. /* redraw bottom line */
  483.         bufout(topline+SCRNL2,SCRNL1,1);
  484.     }
  485.     else    {
  486. /* redraw whole screen */
  487.         bufout(topline,1,SCRNL1);
  488.     }
  489. }
  490.  
  491. /* scroll screen down. topline will be new top line */
  492. edsdn(topline)    int topline;
  493. {    if (outhasdn() == YES)    {
  494. /* hardware scroll */
  495.         outsdn();
  496. /* redraw top line */
  497.         bufout(topline,1,1);
  498.     }
  499.     else    bufout(topline,1,SCRNL1);
  500. }
  501.  
  502. #include ed3a
  503.  
  504. /* end module ed4/ccc */
  505.